<template>
  <view>
  <view class="search-box">
  <input type="search" class="inputBox" placeholder="请输入商品名" 
  v-model="inputValue" @input="input">
   <uni-icons type="closeempty" size="25"  style="margin-right: 10rpx;" @click="cleanSearch"></uni-icons>
  </view>
  <!-- 搜索建议列表 -->
  <view class="suggestList" v-if="searchResults.length!==0">
    <view class="suggestItem" v-for="(item,index) in searchResults" :key="index" @click="gotoDetail(item.goods_id)">
      <view class="goodsname">{{item.goods_name}}</view> 
      <uni-icons type="arrowright" size="16" ></uni-icons>
    </view>
  </view>
  <!-- 搜索历史记录 -->
  <view class="historyBox" v-else>
    <!-- 标题区域 -->
    <view class="historyTitle">
      <text>搜索历史</text>
      <uni-icons type="trash-filled" size="20" @click="cleanHistory"></uni-icons>
    </view>
    <!-- 列表区域 -->
    <view class="historyList">
      <uni-tag :text="item" v-for="(item, i) in historys" :key="i" inverted="true" style="margin-right:20rpx;margin-top: 40rpx;" @click="gotoGoodsList(item)"></uni-tag>
    </view>
  </view>
   </view>
</template>

<script>
  export default {
    data() {
      return {
        inputValue:'',
        timer:null,
        searchResults: [],     // 搜索结果列表
        historyList: []   // 搜索关键词的历史记录
      }
    },
    computed:{
      historys(){
        return [...this.historyList].reverse()
      }
    },
    onLoad(){
    this.historyList= JSON.parse(uni.getStorageSync('kw')||'[]')
    },
    methods: {
      //输入框事件
      input(){
        //清除timer对应的延时器
       clearTimeout(this.timer)
       this.timer=setTimeout(()=>{
          this.getSearchList()
       },500)
      },
      //// 根据搜索关键词，搜索商品建议列表
      async getSearchList(){
        //判断关键词是否为空
        if(this.inputValue===''){
          this.searchResults=[]
          return
        }
        //发起请求，获取搜索建议列表
        const {message} =await this.$u.get("/goods/qsearch",{query:this.inputValue})
        console.log(message)
        this.searchResults=message
        this.saveSearchHistory()
      },
      //跳转到商品详情页
      gotoDetail(goods_id){
        uni.navigateTo({
          url:"/subpkg/goods_detail/goods_detail?goods_id="+goods_id
        })
      },
      //保存搜索关键词的方法
      saveSearchHistory(){
        //把关键字push到历史记录中
        // this.historyList.push(this.inputValue)
        //将Array数组转化为set对象
        const set=new Set(this.historyList)
        //调用set对象的delete方法，移除对应的元素
        set.delete(this.inputValue)
        //调用set对象的add方法，向set中添加元素
        set.add(this.inputValue)
        //将set对象转化为Array数组
        this.historyList=Array.from(set)
        //对搜索记录进行持久化处理
        uni.setStorageSync('kw',JSON.stringify(this.historyList))
      },
      //清除输入框内容的方法
      cleanSearch(){
        this.inputValue=''
        this.searchResults= []
    },
     //清除历史记录
     cleanHistory(){
       this.historyList=[]
       uni.setStorageSync('kw','[]')
     },
     // 点击跳转到商品列表页面
     gotoGoodsList(item){
       uni.navigateTo({
         url:"/subpkg/goods_list/goods_list?query="+item
       })
     }
  }
  }
  
</script>

<style lang="scss" scoped>
.search-box{
  position: sticky;
  top: 0;
  z-index: 999;
  height: 100rpx;
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #EA4350;
}
.inputBox{
  height: 61rpx;
  width: 650rpx;
  border: 1px solid #E7E7E7;
  background-color: #fff;
  border-radius:30rpx ;
  text-align: center;
  margin-left: 15rpx;
}

.suggestList{
  padding: 0 5rpx;
  .suggestItem{
    font-size: 26rpx;
    border-bottom: 1px solid #efefef;
    padding: 13rpx 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
  .goodsname{
    //文字不允许换行（单行文本）
    white-space: nowrap;
    //溢出部分隐藏
    overflow: hidden;
    //文本溢出后，使用...代替
    text-overflow: ellipsis;
    margin-right: 5rpx;
  }
}
.historyBox{
  padding: 0 30rpx;
  .historyTitle{
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 80rpx;
    font-size: 32rpx;
    border-bottom: 1px solid #efefef;
  }
  .historyList{
    display: flex;
    flex-wrap: wrap;
  }
}


</style>
